package com.redteam.xomtro.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.redteam.xomtro.R;
import com.redteam.xomtro.RoomDetailActivity;
import com.redteam.xomtro.entities.Room;

import java.util.ArrayList;
import java.util.List;

public class RoomListAdapter extends BaseAdapter  {

    private final Context act;
    private final Typeface tfMainFont;
    private final Typeface tfRoomFont;
    private final List<Room> roomList;
    private final ImageLoader imgLoader;
    private final DisplayImageOptions options;
    private final DisplayImageOptions optionsAvatar;
    private int index;

    public RoomListAdapter(List<Room> roomList, Context cont, DisplayImageOptions options, DisplayImageOptions optionsAvatar, Typeface titleFont, Typeface eventFont, ImageLoader imgLoader) {
        this.act = cont;
        this.imgLoader = imgLoader;
        this.tfMainFont = titleFont;
        this.tfRoomFont = eventFont;
        this.roomList = roomList;
        this.options = options;
        this.optionsAvatar = optionsAvatar;
    }
    @Override
    public View getView(int position, View v, ViewGroup parent) {
        if (roomList.get(position).getId().equals("null")) {
            View firstSpace = new View(act);
            firstSpace.setTag("null");
            firstSpace.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) act.getResources().getDimension(R.dimen.abc_action_bar_default_height)));
            return firstSpace;
        } else {
            v = View.inflate(act, R.layout.item_room, null);
            TextView roomTitle = (TextView) v.findViewById(R.id.tvRoomTitle);

            GridView grid_view = (GridView) v.findViewById(R.id.grid_view);
            grid_view.setNumColumns(2);
            if (roomList.get(position).getDemo_subImage() != null) {
                SubImgRoomAdapter adapter = new SubImgRoomAdapter(this.act, roomList.get(position).getDemo_subImage());
                grid_view.setAdapter(adapter);
            }else{
                grid_view.setVisibility(View.GONE);
            }
            TextView roomAddress = (TextView) v.findViewById(R.id.tvRoomAddress);
            //TextView roomDescription = (TextView) v.findViewById(R.id.tvRoomDescription);

            index= position;
            //roomTitle.setTypeface(tfMainFont);
            roomAddress.setTypeface(tfRoomFont);
            roomAddress.setOnClickListener(onClickListener);
           // roomDescription.setTypeface(tfMainFont);

            //roomImage.setImageResource(roomList.get(position).getDemo_titleImage());
//            imgLoader.displayImage(eventList.get(position).getBannerUrl(), eventImage, options);

            //roomPoster.setTag(position);
            //roomPoster.setOnClickListener(onClickListener);
            //imgLoader.displayImage(roomList.get(position).getRoom_Image(), roomPoster, optionsAvatar);//getUserProfileAvatar()
//            posterName.setTag(position);
//            posterName.setOnClickListener(onClickListener);
//            posterName.setText(roomList.get(position).getRoom_Name());
            roomTitle.setText(roomList.get(position).getRoom_Name());
            roomTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(act, RoomDetailActivity.class);
                    it.putExtra(RoomDetailActivity.SELECTED_ROOM, (Room)roomList.get(index));
                    act.startActivity(it);
//                    RoomDetailFragment rdf =new RoomDetailFragment(new WellcomeActivity(),roomList.get(index),imgLoader,options, optionsAvatar);
//                    FragmentManager fragmentManager = getFragmentManager();
//                    mf.getFragmentManager().beginTransaction()
//                            .setCustomAnimations(R.anim.fade_in,R.anim.fade_out)
//                            .replace(R.id.content_frame,new RoomDetailFragment(new WellcomeActivity(),roomList.get(index),imgLoader,options, optionsAvatar))
//                            .commit();
//                            (new RoomDetailFragment(new WellcomeActivity(),roomList.get(index),imgLoader,options, optionsAvatar), R.anim.fade_in, R.anim.fade_out);
                }
            });
            //roomDescription.setText(roomList.get(position).getRoom_Description());
            roomAddress.setText(roomList.get(position).getRoom_Address());
//            roomPrice.setText(roomList.get(position).getRoom_Price()+"");
            return v;
        }
    }

    class SubImgRoomAdapter extends BaseAdapter {
        private final ArrayList<Integer> demo_subImage;
        private Context context;

        public SubImgRoomAdapter(Context act, ArrayList<Integer> demo_subImage) {
            this.context = act;
            this.demo_subImage = demo_subImage;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {

            ImageView imageView = new ImageView(this.context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(4,4,4,4);
            if(demo_subImage.size()==4||demo_subImage.size()>4) {
                imageView.setLayoutParams(new GridView.LayoutParams(370, 185));
            }else if(demo_subImage.size()==2||demo_subImage.size()==3){
                imageView.setLayoutParams(new GridView.LayoutParams(362, 365));
            }
            imageView.setImageResource(demo_subImage.get(position));
            return imageView;
        }

        @Override
        public int getCount() {
            if(demo_subImage.size()>4){
                return 4;
            }else if(demo_subImage.size()==3){
                return 2;
            }else if(demo_subImage.size()==2 || demo_subImage.size()==1||demo_subImage.size()==4){
                return demo_subImage.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    };

    @Override
    public int getCount() {
        return roomList.size();
    }

    @Override
    public Room getItem(int position) {
        return roomList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}


